package win.larryzeal.spring.config.custom;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import win.larryzeal.spring.entity.User;
import win.larryzeal.spring.repo.UserRepository;

import java.util.Collection;

/**
 * Created by 张少昆 on 2017/8/30 0030.
 */
//@Service
public class SpringDataUserDetailsService implements UserDetailsService {
	@Autowired
	private UserRepository userRepository;

	@Override
	public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException{
		User user = userRepository.findByUsername(username);//
		return new UserDetails(){

			@Override
			public Collection<? extends GrantedAuthority> getAuthorities(){
				return null;
			}

			@Override
			public String getPassword(){
				return user.getPassword();
			}

			@Override
			public String getUsername(){
				return user.getUsername();
			}

			@Override
			public boolean isAccountNonExpired(){
				return true;
			}

			@Override
			public boolean isAccountNonLocked(){
				return true;
			}

			@Override
			public boolean isCredentialsNonExpired(){
				return false;
			}

			@Override
			public boolean isEnabled(){
				return user.isEnabled();
			}
		};
	}

	class MyUserDetails implements UserDetails{

		@Override
		public Collection<? extends GrantedAuthority> getAuthorities(){
			return null;
		}

		@Override
		public String getPassword(){
			return null;
		}

		@Override
		public String getUsername(){
			return null;
		}

		@Override
		public boolean isAccountNonExpired(){
			return false;
		}

		@Override
		public boolean isAccountNonLocked(){
			return false;
		}

		@Override
		public boolean isCredentialsNonExpired(){
			return false;
		}

		@Override
		public boolean isEnabled(){
			return false;
		}
	}
}
